package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController("userSetmeal")
@RequestMapping("/user/setmeal")
@Api("用户套餐功能")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @return Result<List < Setmeal>>
     * @throws
     * @description 用户根据分类id查询套餐的功能
     * @author Administrator
     * @param[1] categoryId
     * @time 2023/12/1 16:09
     */
    //@ApiOperation("用户根据分类id查询套餐的功能")
    //@GetMapping("/list")
    //public Result<List<Setmeal>> selectSetmealByCategoryId(Long categoryId) {
    //    log.info("正在执行用户根据分类id查询套餐的功能.......分类id为:{}", categoryId);
    //    ValueOperations valueOperations = redisTemplate.opsForValue();
    //    List<Setmeal> setmeals = (List<Setmeal>) valueOperations.get("user_" + categoryId);
    //
    //    if (setmeals == null) {
    //        setmeals = setmealService.selectSetmealByCategoryId(categoryId);
    //    }
    //    return Result.success(setmeals);
    //}
    @ApiOperation("用户根据分类id查询套餐的功能")
    @GetMapping("/list")
    @Cacheable(cacheNames = "userCache", key = "#categoryId")
    public Result<List<Setmeal>> selectSetmealByCategoryId(Long categoryId) {

        log.info("正在执行用户根据分类id查询套餐的功能.......分类id为:{}", categoryId);

        List<Setmeal> setmeals = setmealService.selectSetmealByCategoryId(categoryId);
        setmeals = setmeals.stream().filter(a ->
                a.getStatus() == StatusConstant.ENABLE
        ).collect(Collectors.toList());

        return Result.success(setmeals);
    }

    /**
     * @return Result<List < DishItemVO>>
     * @throws
     * @description 用户根据套餐id查询菜品
     * @author Administrator
     * @param[1] id
     * @time 2023/12/1 16:29
     */

    @ApiOperation("用户根据套餐id查询菜品")
    @GetMapping("/dish/{id}")
    public Result<List<DishItemVO>> selectDishBySetmealId(@PathVariable Long id) {

        log.info("正在执行用户根据套餐id查询菜品的功能.........套餐id为:{}", id);

        List<DishItemVO> dishItemVOS = setmealService.selectDishBySetmealId(id);

        return Result.success(dishItemVOS);
    }

}
